<?php

namespace App\Http\Controllers\Unity;

use App\Http\Controllers\Controller;
use App\Models\Floor;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
use PhpMqtt\Client\MqttClient; // 引入 MQTT 客户端类
use InfluxDB2\Client as Influx; // 引入 MQTT 客户端类


class RoomEquipmentController extends Controller
{
    /**
     * @param Request $request 从列表中选出所有的属性
     * @return \Illuminate\Http\JsonResponse
     */
    public function getFloorInfo(Request $request)
    {
        $val = $request->validate([
            'name' => 'required',
            'type' => 'required'
        ]);
        if ($val['type']) {
            $data = Floor::with(['rooms'])->where(['name' => $val['name']])->get();
        } else {
            $data = Floor::with(['rooms' => function ($query) {
                $query->where(['status' => 1]);
            }])->where(['name' => $val['name']])->get();
        }
        $data = [
            'code' => 200,
            'message' => '',
            'data' => $data,
        ];
        return response()->json($data);
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function getWeather(Request $request, MqttClient $client, Influx $influx)
    {
        try {
            if (Cache::has(env('JUHE_WEATHET_CACHE_KEY'))) {
                    $data = [
                        'code' => 200,
                        'message' => '获取缓存成功',
                        'data' => Cache::get(env('JUHE_WEATHET_CACHE_KEY')),
                    ];
                    return response()->json($data);
            } else {
                $val = $request->validate([
                    'city' => 'required',
                ]);
                $response = Http::asForm()->get(env('JUHE_WEATHER_URL'), ['key' => env('JUHE_WEATHER_KEY'), 'city' => $val['city']]);
                if ($response->ok()) {
                    Cache::put('juhe_weather', $response->json(), $seconds = 24 * 3600);
                    $data = [
                        'code' => 200,
                        'message' => '获取正式数据成功',
                        'data' => $response->json(),
                    ];
                    return response()->json($data);
                }
            }
        } catch (\Exception $e) {
            $data = [
                'code' => 200,
                'message' => '请求异常',
                'data' => '',
            ];
            return response()->json($data);
        }
    }

    /**
     * 改用缓存
     * @return \Illuminate\Http\JsonResponse
     */
    public function getAirQuality(Request $request)
    {
        try {
            if (Cache::has(env('JUHE_AIR_CACHE_KEY'))) {
                $data = [
                    'code' => 200,
                    'message' => '获取缓存成功',
                    'data' => Cache::get(env('JUHE_AIR_CACHE_KEY')),
                ];
                return response()->json($data);
            } else {
                // 根据集合数据接口，三亚市坐标信息为Id": "225","CityName": "三亚市","CityCode": "460200","CityJC": "SYS"， 这里干脆写死
                $response = Http::asForm()->get(env('JUHE_AIR_QUALITY_URL'), ['key' => env('JUHE_AIR_QUALITY'), 'cityId' => 225]);
                if ($response->ok()) {
                    Cache::put('juhe_air', $response->json(), $seconds = 24 * 3600);
                    $data = [
                        'code' => 200,
                        'message' => '获取正式数据成功',
                        'data' => $response->json(),
                    ];
                    return response()->json($data);
                }
            }
        } catch (\Exception $e) {
            $data = [
                'code' => 200,
                'message' => '请求异常',
                'data' => '',
            ];
            return response()->json($data);
        }
    }


}
